import util from '@/libs/util'

// 用户信息初始化和编辑

export default {
  data () {
    return {
      sender: {},
      userDialog: false,
      userInfoTemp: null,
    }
  },
  created () {
    this.initGuest()
  },
  methods: {
    initGuest () {
      let sender = localStorage.getItem('daisy_guest')
      if (sender) {
        this.sender = JSON.parse(sender)
      } else {
        this.sender = {
          id: util.guid(),
          name: randomAnimal(),
          head: ''
        }
        localStorage.setItem('daisy_guest', JSON.stringify(this.sender))
      }
    },

    openUserInfoDialog () {
      this.userInfoTemp = Object.assign({}, this.sender)
    },

    getGravatar (email) {
      return `https://www.gravatar.com/avatar/${MD5(email.trim().toLowerCase())}?s=64&d=retro`
    },

    convertUserHead () {
      let gravatar = this.getGravatar(this.sender.head)
      let sender = JSON.parse(JSON.stringify(this.sender))
      sender.head = gravatar
      return sender
    },

    updateUserInfo () {
      this.sender.name = this.userInfoTemp.name
      this.sender.head = this.userInfoTemp.head
      localStorage.setItem('daisy_guest', JSON.stringify(this.sender))
      this.userDialog = false
    },
  },
}
